#include "metaparser.h"
#include <QFile>
#include <QJsonDocument>
#include <QJsonArray>
#include <QDebug>
#include <QMessageBox>

MetaParser::MetaParser(QString metaPath) : metaPath(metaPath)
{
    loadMetadata();
    qDebug() << metaContent;
}

MetaParser::MetaParser()
{

}

void MetaParser::setMetaPath(QString metaPath)
{
    this->metaPath = metaPath;
    loadMetadata();
    qDebug() << metaContent;
}

QString MetaParser::getMetaPath()
{
    return metaPath;
}

void MetaParser::loadMetadata()
{
    QFile metaFile(metaPath);
    metaFile.open(QIODevice::ReadOnly | QIODevice::Text);
    metaContent = metaFile.readAll();
    metaFile.close();
}

void MetaParser::parseMetadata()
{

    QJsonParseError parseJsonErr;
    QJsonDocument document = QJsonDocument::fromJson(metaContent.toUtf8(), &parseJsonErr);
    if (!(parseJsonErr.error == QJsonParseError::NoError)) {
        QMessageBox::about(NULL, "WARNING", "Configuration File Exception in Meta parser!");
        return;
    }
    metaObj = document.object();
    name = parseMetaObj("meta", "name");
    creator = parseMetaObj("meta", "creator");
    instruments = parseMetaObj("meta","instruments");
    createDate = parseMetaObj("meta","createDate");
    location = parseMetaObj("meta","location");
    comments = parseMetaObj("meta","comments");
    setsObj = parseMetaObj("meta", "sets").toObject();
    type = parseMetaObj("sets", "type");
    format = parseMetaObj("sets", "format");
    origin = parseMetaObj("sets", "origin");
    history = parseMetaObj("sets", "history").toArray();
    getAttrObj();
}

QJsonValue MetaParser::parseMetaObj(QString obj, QString key)
{
    if (metaObj.empty())
    {
        return QJsonValue("");
    }
    if (obj == "meta"){
        if (metaObj.contains(key))
        {
            return metaObj.value(key);
        } else {
    //        QMessageBox::warning(NULL, "WARNING", "No key named " + key);
            return QJsonValue("");
        }
    } else if (obj == "sets"){
        if (setsObj.contains(key))
        {
            return setsObj.value(key);
        } else {
            return QJsonValue("");
        }
    }
    return QJsonValue("");
}

void MetaParser::getAttrObj()
{
    newAttr = new QJsonObject;
    QJsonObject::Iterator it;
    for(it = metaObj.begin(); it != metaObj.end(); it++)
    {
        if (it.key() != "name" && it.key() != "creaotr" && it.key() != "createDate" &&
                it.key() != "sets" && it.key() != "instruments" && it.key() != "comments"
                && it.key() != "location")
            newAttr->insert(it.key(), it.value());
    }
}

QString MetaParser::toString(QJsonValue val)
{
    if(val.isArray() || val.isObject()){
       return QString(QJsonDocument(val.toObject()).toJson(QJsonDocument::Compact));
    }else if(val.isBool()){
        if(val.toBool())
            return QString("True");
        else
            return QString("False");
    } else if(val.isNull() || val.isUndefined()){
        return QString("");
    } else if(val.isDouble()){
        return  QString::number(val.toInt());
    } else if(val.isString()){
        return val.toString();
    }
}
